<?php require_once(BASEPATH.'application/controllers/validaciones.php');
class aran extends Controller {

	function aran(){
		parent::Controller();
		$this->load->library('rapyd');
		
}
	function index(){
		redirect("import/aran/filteredgrid");
	}
	function filteredgrid(){
		
				
		$this->rapyd->load("datafilter","datagrid2");
		$this->rapyd->uri->keep_persistence();
		
			
		$filter = new DataFilter("B&uacute;squeda de Aranceles","aran");
		
        $filter->codigo =  new inputField('C&oacute;digo', 'codigo');
		$filter->codigo->size = '20';
		$filter->codigo->maxlength=15;
   
		$filter->descrip =  new inputField('Descripci&oacute;n', 'descrip');
		$filter->descrip->size = '20';

		$filter->unidad = new dropdownField('Unidad','unidad');
		$filter->unidad->style='width:180px;';
		$filter->unidad->option('','Seleccionar');
		$filter->unidad->options('SELECT TRIM(unidades) AS cod, unidades AS valor FROM unidad ORDER BY unidades');		
													
		$filter->buttons("reset","search");
		$filter->build();
        
        $grid = new DataGrid2();
		$grid->order_by("descrip","DESC");
		$grid->per_page=20;  
		
		$uri = anchor('import/aran/dataedit/show/<#id#>','<#codigo#>');
    
        $grid->column('Codigo'  ,$uri);
		$grid->column('Descripcion'  ,'descrip');
        $grid->column('Unidad'   ,'unidad','align=\'center\'');
        $grid->column('Tarifa'   ,'tarifa','align=\'center\'');
             		         
		$grid->add('import/aran/dataedit/create','Nuevo Arancel');
		$grid->build('datagridST');
		$grid->output;
		
		$style ='
		<style type="text/css">
		.fakeContainer { /* The parent container */
		    margin: 5px;
		    padding: 0px;
		    border: none;
		    width: 1100px; /* Required to set */
		    height: 480px; /* Required to set */
		    overflow: hidden; /* Required to set */
		}
		</style>	
		';

		$data['style']   = $style;
		$data['style']  .= style('superTables.css');
		
		$data['filtro']  = $filter->output;
		$data['content'] = $grid->output;
		$data['title']   = heading("Impuesto Arancelario");
		$data['script']  = script('jquery.js');
		$data["script"] .= script('superTables.js');
		$data['head']    = $this->rapyd->get_head();

		$this->load->view('view_ventanas', $data);
	}

	function dataedit(){
		
		$this->rapyd->load('dataedit');

		$script='
		$(function() {
			$(".inputnum").numeric(".");
		});';

		$edit = new DataEdit('Arancel','aran');
		$edit->back_url = site_url("import/aran/filteredgrid");
		
		$edit->script($script,'create');
		$edit->script($script,'modify');

		$edit->pre_process( 'delete','_pre_delete' );
		$edit->post_process('insert','_post_insert');
		$edit->post_process('update','_post_update');
		$edit->post_process('delete','_post_delete');

		$edit->codigo =  new inputField('C&oacute;digo', 'codigo');
		$edit->codigo->mode='autohide';
		$edit->codigo->rule ='trim|strtoupper|required';
		$edit->codigo->size = '20';
		$edit->codigo->maxlength=15;

		$edit->descrip =  new inputField('Descripci&oacute;n', 'descrip');
		$edit->descrip->rule ='trim|strtoupper|required';

		$edit->unidad = new dropdownField('Unidad','unidad');
		$edit->unidad->rule ='required';
		$edit->unidad->style='width:180px;';
		$edit->unidad->option('','Seleccionar');
		$edit->unidad->options('SELECT TRIM(unidades) AS cod, unidades AS valor FROM unidad ORDER BY unidades');

		$edit->tarifa = new inputField('Tarifa', 'tarifa');
		$edit->tarifa->size = 10;
		$edit->tarifa->maxlength=10;
		$edit->tarifa->css_class='inputnum';
		$edit->tarifa->rule='callback_positivo|numeric|required';

		//$edit->dolar = new inputField('D&oacute;lar', 'dolar');
		//$edit->dolar->size = 10;
		//$edit->dolar->maxlength=10;
		//$edit->dolar->css_class='inputnum';
		//$edit->dolar->rule='callback_positivo|numeric';

		$edit->buttons('modify','save','undo','delete','back');
		$edit->build();
		
		$data['content'] = $edit->output;
	
		$data["script"]   = script("jquery.js");
		$data['style']	 = style("jquery.alerts.css");
		$data['style']	.= style("redmond/jquery-ui.css");		
		$data["head"]    = $this->rapyd->get_head();
		$data['title']   = heading($edit->descrip->value);
		$this->load->view('view_ventanas', $data); 

	}

	function _pre_delete($do) {
		$codigo=$this->db->escape($do->get('codigo'));
		$chek =  $this->datasis->dameval("SELECT COUNT(*) FROM itordi WHERE codaran=${codigo}");
		if ($chek > 0){
			$do->error_message_ar['pre_del'] = $do->error_message_ar['delete']='El arancel a borra contiene productos relacionados, por ello no puede ser eliminado.';
			return false;
		}
		return true;
	}
	function _post_delete($do){
		$codigo=$do->get('codigo');
		logusu('aran',"ARANCEL ${codigo} ELIMINADO");
	}

	function _post_insert($do){
		$codigo=$do->get('codigo');
		logusu('aran',"ARANCEL ${codigo} CREADO");
	}

	function _post_update($do){
		$codigo=$do->get('codigo');
		logusu('aran',"ARANCEL ${codigo} MODIFICADO");
	}
}
